// pages/list/list.js
const api = require('../../api/api.js')
Page({
  data: {
    method: '',
    films: {},
    start: 0,
    count: 12,
    total: 0,
    showLoading: false,
    showNomore: false
  },
  onLoad: function (option) {
    this.data.method = option.method
  },
  onReady: function () {
    this.loadListData();
  },
  loadListData () {
    return api[this.data.method]({
      start: this.data.start,
      count: this.data.count
    }).then(data => {
      const list = this.data.films.list || []
      const films = {
        title: data.subject_collection.name,
        list: list.concat(data.subject_collection_items)
      }
      this.setData({
        films,
        start: this.data.start + this.data.count,
        total: data.total
      })
    }).catch(api.showError)
  },
  // 上拉加载
  onReachBottom () {
    if (this.data.start < this.data.total) {
      this.setData({
        showLoading: true
      })
      this.loadListData().then(() => {
        this.setData({
          showLoading: false
        })
      })
    } else {
      this.setData({
        showNomore: true
      })
    }
    
  },
  onPullDownRefresh () {
    this.setData({
      start: 0,
      films: {}
    })
    this.loadListData()
  }
})
